﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.CmsContents;
using Module.Core.BusinessLogic.Products;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.EntitiesInfo.TablesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using System.Data.SqlClient;

namespace Web.Cms.ProductCart
{
    public partial class ProductCartManager : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private int page;
        private int cPage = 1;
        private DateTime dateTo;
        private DateTime dateFrom;
        private int from, to;
        ProductCartInfo info = new ProductCartInfo();
        #endregion

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);
                if (Request.QueryString["dateFrom"] != null && Request.QueryString["dateFrom"] != "")
                {
                    dateFrom = Convert.ToDateTime(Request.QueryString["dateFrom"].Trim());
                    txtDatefrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }
                else
                {
                    dateFrom = DateTime.Now.AddYears(-2);
                    txtDatefrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }

                if (Request.QueryString["dateTo"] != null && Request.QueryString["dateTo"] != "")
                {
                    dateTo = Convert.ToDateTime(Request.QueryString["dateTo"].Trim());
                    txtDateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }
                else
                {
                    dateTo = DateTime.Now.AddYears(+1);
                    txtDateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                page = int.Parse("0" + Request.QueryString["page"]);
                if (page == 0) page = 1;
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
           

        }

        #region LoadData
        private void LoadData()
        {

            totalRecords = ProductCartBL.SelectCount(true);
            lbTotal.Text = "Tổng số bản ghi: " + ProductCartBL.GetCount(DateTime.Parse(txtDatefrom.Text), DateTime.Parse(txtDateTo.Text));
            DataSet ds;
            ds = ProductCartBL.ListProductCartPage(info.CartId, info.ProductId, info.MemberId, DateTime.Parse(txtDatefrom.Text), DateTime.Parse(txtDateTo.Text), page, Const.pageSize30, 15, totalRecords, "Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey + "&dateFrom=" + dateFrom + "&dateTo=" + dateTo + "&page");
            if (ds != null)
            {
                DataTable dtbData = ds.Tables[0];
                DataTable dtbPhanTrang = ds.Tables[1];
                if (dtbData.Rows.Count > 0)
                {

                    grdProductCart.DataSource = dtbData;
                    grdProductCart.RowDataBound += new GridViewRowEventHandler(grdProductCart_RowDataBound);
                    grdProductCart.DataBind();
                    if (dtbPhanTrang.Rows.Count > 0)
                    {
                        
                        ltlPhanTrang.Text = dtbPhanTrang.Rows[0]["PhanTrang"] + "";
                        
                    }
                }
            }
        }
        #endregion

        protected void grdProductCart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "Commanddelete":
                        try
                        {
                            int cartId = Convert.ToInt32(e.CommandArgument);
                            ProductCartBL.Delete(cartId);
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void grdProductCart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Text = (Const.pageSize30 * (page - 1) + e.Row.DataItemIndex + 1).ToString();
        }


        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey  + "&dateFrom=" + txtDatefrom.Text.Trim() + "&dateTo=" + txtDateTo.Text.Trim() + "&page=" + page);
          

        }


    }
}